home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_guile.idb / usr / freeware / share / guile / 1.4 / ice-9 / common-list.scm.z / common-list.scm
Text File  |  2002-07-08  |  8KB  |  231 lines

  1. ;;;; common-list.scm --- COMMON LISP list functions for Scheme
  2. ;;;;
  3. ;;;;     Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
  4. ;;;; 
  5. ;;;; This program is free software; you can redistribute it and/or modify
  6. ;;;; it under the terms of the GNU General Public License as published by
  7. ;;;; the Free Software Foundation; either version 2, or (at your option)
  8. ;;;; any later version.
  9. ;;;; 
  10. ;;;; This program is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;;;; GNU General Public License for more details.
  14. ;;;; 
  15. ;;;; You should have received a copy of the GNU General Public License
  16. ;;;; along with this software; see the file COPYING.  If not, write to
  17. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. ;;;; Boston, MA 02111-1307 USA
  19. ;;;; 
  20.  
  21. (define-module (ice-9 common-list))
  22.  
  23. ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
  24. ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
  25. ;
  26. ;Permission to copy this software, to redistribute it, and to use it
  27. ;for any purpose is granted, subject to the following restrictions and
  28. ;understandings.
  29. ;
  30. ;1.  Any copy made of this software must include this copyright notice
  31. ;in full.
  32. ;
  33. ;2.  I have made no warrantee or representation that the operation of
  34. ;this software will be error-free, and I am under no obligation to
  35. ;provide any services, by way of maintenance, update, or otherwise.
  36. ;
  37. ;3.  In conjunction with products arising from the use of this
  38. ;material, there shall be no use of my name in any advertising,
  39. ;promotional, or sales literature without prior written consent in
  40. ;each case.
  41.  
  42. (define-public (adjoin e l) 
  43.   "Returns list L, possibly with element E added if it is not already in L."
  44.   (if (memq e l) l (cons e l)))
  45.  
  46. (define-public (union l1 l2)
  47.   "Returns a new list that is the union of L1 and L2.
  48. Elements that occur in both lists will occur only once
  49. in the result list."
  50.   (cond ((null? l1) l2)
  51.     ((null? l2) l1)
  52.     (else (union (cdr l1) (adjoin (car l1) l2)))))
  53.  
  54. (define-public (intersection l1 l2)
  55.   "Returns a new list that is the intersection of L1 and L2.
  56. Only elements that occur in both lists will occur in the result list."
  57.   (cond ((null? l1) l1)
  58.     ((null? l2) l2)
  59.     ((memv (car l1) l2) (cons (car l1) (intersection (cdr l1) l2)))
  60.     (else (intersection (cdr l1) l2))))
  61.  
  62. (define-public (set-difference l1 l2)
  63.   "Return elements from list L1 that are not in list L2."
  64.   (cond ((null? l1) l1)
  65.     ((memv (car l1) l2) (set-difference (cdr l1) l2))
  66.     (else (cons (car l1) (set-difference (cdr l1) l2)))))
  67.  
  68. (define-public (reduce-init p init l)
  69.   "Same as `reduce' except it implicitly inserts INIT at the start of L."
  70.   (if (null? l)
  71.       init
  72.       (reduce-init p (p init (car l)) (cdr l))))
  73.  
  74. (define-public (reduce p l)
  75.   "Combines all the elements of sequence L using a binary operation P.
  76. The combination is left-associative. For example, using +, one can
  77. add up all the elements. `reduce' allows you to apply a function which
  78. accepts only two arguments to more than 2 objects.  Functional
  79. programmers usually refer to this as foldl."
  80.   (cond ((null? l) l)
  81.     ((null? (cdr l)) (car l))
  82.     (else (reduce-init p (car l) (cdr l)))))
  83.  
  84. (define-public (some pred l . rest)
  85.   "PRED is a boolean function of as many arguments as there are list
  86. arguments to `some'. I.e., L plus any optional arguments. PRED is
  87. applied to successive elements of the list arguments in order. As soon
  88. as one of these applications returns a true value, `some' terminates
  89. and returns that value.  If no application returns a true value,
  90. `some' returns #f. All the lists should have the same length."
  91.   (cond ((null? rest)
  92.      (let mapf ((l l))
  93.        (and (not (null? l))
  94.         (or (pred (car l)) (mapf (cdr l))))))
  95.     (else (let mapf ((l l) (rest rest))
  96.         (and (not (null? l))
  97.              (or (apply pred (car l) (map car rest))
  98.              (mapf (cdr l) (map cdr rest))))))))
  99.  
  100. (define-public (every pred l . rest)
  101.   "Return #t iff every application of PRED to L, etc., returns #t.
  102. Analogous to `some' except it returns #t if every application of
  103. PRED is #t and #f otherwise."
  104.   (cond ((null? rest)
  105.      (let mapf ((l l))
  106.        (or (null? l)
  107.            (and (pred (car l)) (mapf (cdr l))))))
  108.     (else (let mapf ((l l) (rest rest))
  109.         (or (null? l)
  110.             (and (apply pred (car l) (map car rest))
  111.              (mapf (cdr l) (map cdr rest))))))))
  112.  
  113. (define-public (notany pred . ls) 
  114.   "Return #t iff every application of PRED to L, etc., returns #f.
  115. Analogous to some but returns #t if no application of PRED returns a
  116. true value or #f as soon as any one does."
  117.   (not (apply some pred ls)))
  118.  
  119. (define-public (notevery pred . ls) 
  120.   "Return #t iff there is an application of PRED to L, etc., that returns #f.
  121. Analogous to some but returns #t as soon as an application of PRED returns #f,
  122. or #f otherwise."
  123.   (not (apply every pred ls)))
  124.  
  125. (define-public (find-if pred l)
  126.   "Searches for the first element in L such that (PRED element)
  127. returns true. If it finds any such element in L, element is
  128. returned. Otherwise, #f is returned."
  129.   (cond ((null? l) #f)
  130.     ((pred (car l)) (car l))
  131.     (else (find-if pred (cdr l)))))
  132.  
  133. (define-public (member-if pred l)
  134.   "Returns L if (T element) is true for any element in L.  Returns #f
  135. if PRED does not apply to any element in L."
  136.   (cond ((null? l) #f)
  137.     ((pred (car l)) l)
  138.     (else (member-if pred (cdr l)))))
  139.  
  140. (define-public (remove-if p l)
  141.   "Removes all elements from L where (P element) is true.
  142. Returns everything that's left."
  143.   (cond ((null? l) '())
  144.     ((p (car l)) (remove-if p (cdr l)))
  145.     (else (cons (car l) (remove-if p (cdr l))))))
  146.  
  147. (define-public (remove-if-not p l)
  148.   "Removes all elements from L where (P element) is #f.
  149. Returns everything that's left."
  150.   (cond ((null? l) '())
  151.     ((not (p (car l))) (remove-if-not p (cdr l)))
  152.     (else (cons (car l) (remove-if-not p (cdr l))))))
  153.  
  154. (define-public (delete-if! pred list)
  155.   "Destructive version of `remove-if'."
  156.   (let delete-if ((list list))
  157.     (cond ((null? list) '())
  158.       ((pred (car list)) (delete-if (cdr list)))
  159.       (else
  160.        (set-cdr! list (delete-if (cdr list)))
  161.        list)))) 
  162.  
  163. (define-public (delete-if-not! pred list)
  164.   "Destructive version of `remove-if-not'."
  165.   (let delete-if-not ((list list))
  166.     (cond ((null? list) '())
  167.       ((not (pred (car list))) (delete-if-not (cdr list)))
  168.       (else
  169.        (set-cdr! list (delete-if-not (cdr list)))
  170.        list))))
  171.  
  172. (define-public (butlast lst n)
  173.   "Return all but the last N elements of LST."
  174.   (letrec ((l (- (length lst) n))
  175.        (bl (lambda (lst n)
  176.          (cond ((null? lst) lst)
  177.                ((positive? n)
  178.             (cons (car lst) (bl (cdr lst) (+ -1 n))))
  179.                (else '())))))
  180.     (bl lst (if (negative? n)
  181.         (error "negative argument to butlast" n)
  182.         l))))
  183.  
  184. (define-public (and? . args)
  185.   "Return #t iff all of ARGS are true."
  186.   (cond ((null? args) #t)
  187.     ((car args) (apply and? (cdr args)))
  188.     (else #f)))
  189.  
  190. (define-public (or? . args)
  191.   "Return #t iff any of ARGS is true."
  192.   (cond ((null? args) #f)
  193.     ((car args) #t)
  194.     (else (apply or? (cdr args)))))
  195.  
  196. (define-public (has-duplicates? lst)
  197.   "Return #t iff 2 members of LST are equal?, else #f."
  198.   (cond ((null? lst) #f)
  199.     ((member (car lst) (cdr lst)) #t)
  200.     (else (has-duplicates? (cdr lst)))))
  201.  
  202. (define-public (pick p l)
  203.   "Apply P to each element of L, returning a list of elts
  204. for which P returns a non-#f value."
  205.   (let loop ((s '())
  206.          (l l))
  207.     (cond
  208.      ((null? l)     s)
  209.      ((p (car l))    (loop (cons (car l) s) (cdr l)))
  210.      (else        (loop s (cdr l))))))
  211.  
  212. (define-public (pick-mappings p l)
  213.   "Apply P to each element of L, returning a list of the 
  214. non-#f return values of P."
  215.   (let loop ((s '())
  216.          (l l))
  217.     (cond
  218.      ((null? l)     s)
  219.      ((p (car l)) =>    (lambda (mapping) (loop (cons mapping s) (cdr l))))
  220.      (else        (loop s (cdr l))))))
  221.  
  222. (define-public (uniq l)
  223.   "Return a list containing elements of L, with duplicates removed."
  224.   (if (null? l)
  225.       '()
  226.       (let ((u (uniq (cdr l))))
  227.     (if (memq (car l) u)
  228.         u
  229.         (cons (car l) u)))))
  230.  
  231.